package cn.school.servlet;

import cn.school.entity.Student;
import cn.school.util.StringUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

/**
 * @author oldliu
 * @since 1.0
 */
@WebServlet("/addStudentServlet")
public class AddStudentServlet extends HttpServlet {

    //    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.doPost(req, resp);
//    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        Student stu = new Student();
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            for (FileItem fi : fileItems) {
                if (fi.isFormField()) {
                    //普通的表单域
                    out.print("<h1>名字是：" + fi.getFieldName() + ",值是" + fi.getString("utf-8") + "</h1>");
                    switch (fi.getFieldName()) {
                        case "sname":
                            stu.setSname(fi.getString("utf-8"));
                            break;
                        case "sex":
                            stu.setSex(fi.getString("utf-8"));
                            break;
                        case "gradeid":
                            stu.setGradeid(StringUtil.toLong(fi.getString("utf-8")));
                            break;
                    }
                } else {
                    //文件域
                    if (fi.getSize() < 1)
                        continue;
                    out.print("<h1>文件域的名字是：" + fi.getFieldName() + ",文件名是：" + fi.getName() + "</h1>");
                    String picpath = request.getServletContext().getRealPath("images");
                    File file = new File(picpath);
                    if (!file.exists())
                        file.mkdirs();
                    //1:改名字,保留后缀
                    //2:文件的路径，需要保存到数据库中。。。
                    String suffix = fi.getName().substring(fi.getName().lastIndexOf('.'));
                    if (!(suffix.equals(".jpg") || suffix.equals(".png"))) {
                        //throw new
                    }
                    String newName = UUID.randomUUID() + suffix;
                    fi.write(new File(file, newName));
                    out.print("<img src='images/" + newName + "' height='399px' width='400px'/>");
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
